package fms.services.config;

import java.io.IOException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

import fms.services.PropertyTool;
import fms.services.ResourceManager;

public class FmsConfiger {
    
    private static FmsConfiger config = null;
    
    private static Logger log = Logger
            .getLogger(FmsConfiger.class.getName());
    
    private static final String DBSERVER_URL = "DBSERVER_URL";
    
    private static final String DBSERVER_DRIVER = "DBSERVER_DRIVER";
    
    private static final String MAILSERVER_URL = "MAILSERVER_URL";
    
    private static final String MAILSERVER_USER = "MAILSERVER_USER";
    
    private static final String MAILSERVER_PASS = "MAILSERVER_PASS";
    
    private static final String DBSERVER_USINGPOOL = "DBSERVER_USINGPOOL";
    
    private static final String DBSERVER_POOLNAME = "DBSERVER_POOLNAME";

	private static final String MAILFROM_URL = "MAILFROM_URL";
    
    private static String dbServerURL = "";
    
    private static String mailServerURL = "";
    
    private static String mailFromURL = "";
    
    private static String mailServerUser = "";
    
    private static String mailServerPass = "";
    
    private static String dbServerDriver = "";
    
    private static String dbConnUsingPool = "";
    
    private static String dbConnPoolName = "";
    
    private Properties prop = null;
    
    public synchronized static FmsConfiger getInstance() {
        if (config == null) {
            config = new FmsConfiger();
        }
        return config;
    }
    
    private FmsConfiger() {
    }
    
    private synchronized void readConfig() {
        try {
            prop = PropertyTool.loadProperty(ResourceManager.PROPERTY_FILE_NAME_FMSWEB);
        } catch (IOException ex) {
            log.log(Level.WARNING,"Reading config file failed...try create new one "+ ResourceManager.PROPERTY_FILE_NAME_FMSWEB);
            try {
                PropertyTool.createProperty(this.buildDefaultProperty(),ResourceManager.PROPERTY_FILE_NAME_FMSWEB);
                log.log(Level.WARNING, "create new one succeed!"+ ResourceManager.PROPERTY_FILE_NAME_FMSWEB);
                prop = PropertyTool.loadProperty(ResourceManager.PROPERTY_FILE_NAME_FMSWEB);
            } catch (IOException exp) {
                log.log(Level.SEVERE,"create new one failed! using default (memory model,ready only!)"+ ResourceManager.PROPERTY_FILE_NAME_FMSWEB);
            }
        }
        this.applyConfig(prop);
    }
    
    private Properties buildDefaultProperty() {
        Properties pro = new Properties();
        pro.setProperty(FmsConfiger.DBSERVER_DRIVER,
                "com.mysql.jdbc.Driver");
        pro.setProperty(FmsConfiger.DBSERVER_URL,
                "jdbc:mysql://localhost:3306/gymdb?user=root&password=yousufu");
        pro.setProperty(FmsConfiger.MAILSERVER_URL, "nlsde.buaa.edu.cn");
        pro.setProperty(FmsConfiger.MAILSERVER_USER, "huangkai");
        pro.setProperty(FmsConfiger.MAILSERVER_PASS, "1");
        pro.setProperty(FmsConfiger.DBSERVER_USINGPOOL, "false");
        pro.setProperty(FmsConfiger.DBSERVER_POOLNAME, "jdbc/gymdb_dbconn");
        return pro;
    }
    
    private void applyConfig(Properties pro) {
    	FmsConfiger.dbServerURL = pro
                .getProperty(FmsConfiger.DBSERVER_URL);
    	FmsConfiger.mailServerURL = pro
                .getProperty(FmsConfiger.MAILSERVER_URL);
    	FmsConfiger.mailFromURL = pro
        .getProperty(FmsConfiger.MAILFROM_URL);
    	FmsConfiger.mailServerUser = pro
                .getProperty(FmsConfiger.MAILSERVER_USER);
    	FmsConfiger.mailServerPass = pro
                .getProperty(FmsConfiger.MAILSERVER_PASS);
    	FmsConfiger.dbServerDriver = pro
                .getProperty(FmsConfiger.DBSERVER_DRIVER);
    	FmsConfiger.dbConnUsingPool = pro
                .getProperty(FmsConfiger.DBSERVER_USINGPOOL);
    	FmsConfiger.dbConnPoolName = pro
                .getProperty(FmsConfiger.DBSERVER_POOLNAME);
    }
    
    public synchronized String getDBConnPoolName() {
        this.readConfig();
        return FmsConfiger.dbConnPoolName;
    }
    
    public synchronized boolean isDBConnUsingPool() {
        this.readConfig();
        if (FmsConfiger.dbConnUsingPool.equals("true")) {
            return true;
        } else {
            return false;
        }
    }
    
    public synchronized String getMailServerUser() {
        this.readConfig();
        return mailServerUser;
    }
    
    public synchronized String getMailServerURL() {
        this.readConfig();
        return mailServerURL;
    }
    
    public synchronized String getMailFromURL() {
        this.readConfig();
        return mailFromURL;
    }
    
    public synchronized String getMailServerPass() {
        this.readConfig();
        return mailServerPass;
    }
    
    public synchronized String getDBServerDriver() {
        this.readConfig();
        return dbServerDriver;
    }
    
    public synchronized String getDBServerURL() {
        this.readConfig();
        //log.log(Level.INFO, "DB url:" + dbServerURL);
        return dbServerURL;
    }
    
    public static void main(String[] args) {
        System.out.println(FmsConfiger.getInstance().getDBServerURL());
    }
    
}
